<?php 
class ControllerLocalisationCountryCity extends Controller {
	private $error = array(); 

	public function index() {
		$this->load->language('localisation/country_city');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('localisation/country_city');
		$this->getList();
	}

	public function insert() {
		$this->load->language('localisation/country_city');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('localisation/country_city');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_localisation_country_city->addCountryCity($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('localisation/country_city', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm_text();
		$this->getForm_insert();
	}

	public function update() {
		$this->load->language('localisation/country_city');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('localisation/country_city');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_localisation_country_city->editCountryCity($this->request->get['country_city_id'], $this->request->post);			
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('localisation/country_city', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm_text();
		$this->getForm_update();
	}

	private function getList() {
		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('localisation/country_city', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('localisation/country_city/insert', 'token=' . $this->session->data['token'], 'SSL');
		
		$url = '';
		if (isset($this->request->get['filter_country_id'])) {
			$filter_country_id = $this->request->get['filter_country_id'];
			$url .= '&filter_country_id=' . $this->request->get['filter_country_id'];
		} else {
			$filter_country_id = null;
		}
		if (isset($this->request->get['filter_country_zone_id'])) {
			$filter_country_zone_id = $this->request->get['filter_country_zone_id'];
			$url .= '&filter_country_zone_id=' . $this->request->get['filter_country_zone_id'];
		} else {
			$filter_country_zone_id = null;
		}
		if (isset($this->request->get['filter_code'])) {
			$filter_code = $this->request->get['filter_code'];
			$url .= '&filter_code=' . $this->request->get['filter_code'];
		} else {
			$filter_code = null;
		}
		if (isset($this->request->get['filter_postcode'])) {
			$filter_postcode = $this->request->get['filter_postcode'];
			$url .= '&filter_postcode=' . $this->request->get['filter_postcode'];
		} else {
			$filter_postcode = null;
		}
		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}
		if (isset($this->request->get['filter_display_name'])) {
			$filter_display_name = $this->request->get['filter_display_name'];
			$url .= '&filter_display_name=' . $this->request->get['filter_display_name'];
		} else {
			$filter_display_name = null;
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
				
		$this->data['country_citys'] = array();
		$data = array(
			'country_id'   => $filter_country_id,
			'country_zone_id'   => $filter_country_zone_id,
			'code'         => $filter_code,
			'postcode'     => $filter_postcode,
			'status'       => $filter_status,
			'display_name' => $filter_display_name,
			'start'        => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'        => $this->config->get('config_admin_limit')
		);
		$country_city_total = $this->model_localisation_country_city->getTotalCountryCitys($data);
		$results = $this->model_localisation_country_city->getCountryCitys($data);
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('localisation/country_city/update', 'token=' . $this->session->data['token'] . '&country_city_id=' . $result['country_city_id'], 'SSL')
			);
			$this->data['country_citys'][] = array(
				'country_id'       => $result['country_id'],
				'country_zone_id'  => $result['country_zone_id'],
				'name'             => $result['name'],
				'code'             => $result['code'],
				'postcode'         => $result['postcode'],
				'time_difference'  => $result['time_difference'],
				'display_name'     => $result['display_name'],
				'status'           => $result['status'],
				'country_display_name' => $result['country_display_name'],
				'zone_display_name' => $result['zone_display_name'],
				'action'           => $action			
			);
		}
	
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_all'] = $this->language->get('text_all');
		
		$this->data['column_country_id'] = $this->language->get('column_country_id');
		$this->data['column_country_name'] = $this->language->get('column_country_name');
		$this->data['column_zone_id'] = $this->language->get('column_zone_id');
		$this->data['column_zone_name'] = $this->language->get('column_zone_name');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_code'] = $this->language->get('column_code');
		$this->data['column_postcode'] = $this->language->get('column_postcode');
		$this->data['column_time_difference'] = $this->language->get('column_time_difference');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');	

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$pagination = new Pagination();
		$pagination->total = $country_city_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('localisation/country_city', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();
		
		$this->data['filter_country_id'] = $filter_country_id;
		$this->data['filter_country_zone_id'] = $filter_country_zone_id;
		$this->data['filter_code'] = $filter_code;
		$this->data['filter_postcode'] = $filter_postcode;
		$this->data['filter_status'] = $filter_status == null ? -1 : $filter_status;
		$this->data['filter_display_name'] = $filter_display_name;
		
		$this->data['token'] = $this->session->data['token'];
		
		$this->template = 'localisation/country_city_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getForm_text() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['entry_country'] = $this->language->get('entry_country');
		$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_display_name'] = $this->language->get('entry_display_name');
		$this->data['entry_code'] = $this->language->get('entry_code');
		$this->data['entry_postcode'] = $this->language->get('entry_postcode');
		$this->data['entry_time_difference'] = $this->language->get('entry_time_difference');
		$this->data['entry_status'] = $this->language->get('entry_status');
		
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_country_search'] = $this->language->get('text_country_search');
		$this->data['text_country_name'] = $this->language->get('text_country_name');
		$this->data['text_country_display_name'] = $this->language->get('text_country_display_name');
		$this->data['text_country_iso_code'] = $this->language->get('text_country_iso_code');
		$this->data['text_zone_search'] = $this->language->get('text_zone_search');
		$this->data['text_zone_name'] = $this->language->get('text_zone_name');
		$this->data['text_zone_display_name'] = $this->language->get('text_zone_display_name');
		$this->data['text_zone_code'] = $this->language->get('text_zone_code');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),  		
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('localisation/country_city', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
	}

	private function getForm_insert() {
		$this->data['action'] = $this->url->link('localisation/country_city/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('localisation/country_city', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['country_id'] = 0;
		$this->data['country_name'] = "";
		$this->data['country_display_name'] = "";
		$this->data['country_iso_code'] = "";
		
		$this->data['country_zone_id'] = 0;
		$this->data['country_zone_name'] = "";
		$this->data['country_zone_display_name'] = "";
		$this->data['country_zone_code'] = "";
		
		$this->data['name'] = '';
		$this->data['code'] = '';
		$this->data['postcode'] = '';
		$this->data['time_difference'] = 0;
		$this->data['status'] = 0;

		$this->data['country_city_description'] = array();
		
		$this->template = 'localisation/country_city_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function getForm_update() {
		$this->data['action'] = $this->url->link('localisation/country_city/update', 'token=' . $this->session->data['token'] . '&country_city_id=' . $this->request->get['country_city_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('localisation/country_city', 'token=' . $this->session->data['token'], 'SSL');

		$country_city_info = $this->model_localisation_country_city->getCountryCity($this->request->get['country_city_id']);
		$this->data['country_id'] = $country_city_info['country_id'];
		$this->data['country_zone_id'] = $country_city_info['country_zone_id'];
		$this->data['name'] = $country_city_info['name'];
		$this->data['code'] = $country_city_info['code'];
		$this->data['postcode'] = $country_city_info['postcode'];
		$this->data['time_difference'] = $country_city_info['time_difference'];
		$this->data['status'] = $country_city_info['status'];
		
		$this->load->model('localisation/country');
		$country_info = $this->model_localisation_country->getCountryAndDesc($country_city_info['country_id']);
		$this->data['country_name'] = $country_info['name'];
		$this->data['country_display_name'] = $country_info['display_name'];
		$this->data['country_iso_code'] = $country_info['iso_code'];
		
		$this->load->model('localisation/country_zone');
		$country_info = $this->model_localisation_country_zone->getCountryZoneAndDesc($country_city_info['country_zone_id']);
		$this->data['country_zone_name'] = $country_info['name'];
		$this->data['country_zone_display_name'] = $country_info['display_name'];
		$this->data['country_zone_code'] = $country_info['code'];

		$this->data['country_city_description'] = $this->model_localisation_country_city->getCountryCityDescription($this->request->get['country_city_id']);

		$this->template = 'localisation/country_city_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}
}
?>